#### animus figures


rm(list=ls())
library(tidyverse)
library(ggpubr)
library(here)

here::i_am("dot_code_figs/figs-1-4.R")

anes <- read_csv("dot_code_figs/ANES files/anes_timeseries_cdf_csv_20220916.csv")

anes2 <- anes %>%
  select(c(weight_full = VCF0009z, #weights for full sample
           #weight_mode = VCF0009x, # FTF weights DROP 2020
           #weight_web = VCF0009y, # web weights DROP 2020
           weight_post_full = VCF9999, # post-election full weights
           year = VCF0004, # year
           FT_dem = VCF0218 , # Dem Party FT
           FT_rep = VCF0224, # Rep Party FT
           pid = VCF0301, # PID
           mode = VCF0017 #mode of survey, only want 0 (FTF) or 4 (web) later
  )) %>%
  subset(pid != 4 & pid > 0)%>% #drop pure independents and NAs
  sapply(as.numeric) %>% data.frame()


A24 <-  read_csv("C:/Users/sarah/Dropbox/Advances in Political Psychology Article/dot_code_figs/ANES files/anes_timeseries_2024_csv_20250430.csv") #2024 

anes24 <- A24 %>% #2024 ANES
  mutate(year = 2024) %>%     
  select(weight_post_full =  V240107b,   # weights, Post full ANES sample (FTF + panel + web + PAPI)
         weight_full =   V240107a, # 
         #weight_ftf = V240101a, # FTF weights DROP 2020
         #weight_web = V240102a, # web weights DROP 2020
         FT_dem = V241166,
         FT_rep = V241167,
         pid =  V241227x,
         mode = V240002a, #only want 1 (FTF) or 2 (web)
         year) %>%
  subset(pid != 4 & pid > 0)%>% #drop pure independents and NAs
  subset(FT_dem >= 0 & FT_rep >=0)%>%
  sapply(as.numeric) %>% data.frame()

anes3 <- bind_rows(anes2, anes24)

#check
summary(anes3$year) #perf

##drop FT NAs

##########################################################################
#create pid dummy
anes3$pid_dum <- ifelse(anes3$pid < 4, "Democrat", "Republican")

#create inparty and outparty
anes3$inparty <- ifelse(anes3$pid_dum=="Democrat", "Democrat", "Republican")
anes3$outparty <- ifelse(anes3$pid_dum=="Democrat", "Republican", "Democrat")

#link FTs to in and out_party
anes3$inparty_FT <- ifelse(anes3$pid_dum=="Democrat", anes3$FT_dem, anes3$FT_rep)
anes3$outparty_FT <- ifelse(anes3$pid_dum=="Democrat", anes3$FT_rep, anes3$FT_dem)


###caclulate AP as IN_FT - OUT_FT
anes3$AP <- anes3$inparty_FT - anes3$outparty_FT


###################################################
#calculate the weighted average AP for each year

for_fig <- anes3 %>%
  select(year, pid_dum, inparty_FT, outparty_FT, AP, weight_full) %>%
  drop_na()

wav_time_fig <- for_fig %>%
  group_by(year) %>%
  summarise(weighted_av_ap = weighted.mean(AP, weight_full, na.rm=TRUE),
            sd = sd(AP, na.rm=TRUE),
            se = sd / sqrt(n()))

#calculate total n
n_non_na = sum(!is.na(for_fig$AP))

p1<-ggplot(wav_time_fig, aes(x=year, y=weighted_av_ap)) +
  geom_point() +
  geom_line() +
  theme_bw() +
  geom_errorbar(aes(ymin=weighted_av_ap - 1.96*se,
                    ymax=weighted_av_ap + 1.96*se),
                width=0.4) +
  labs(x="Year", y="In-Party - Out-Party FT",
       subtitle="A",
       # title = "Average Affective Polarization by Year",
       caption="ANES (N=44,009)
Means and 95% CIs")+
  scale_y_continuous(limits=c(0,100))
#ggsave("C:/Users/sarah/Dropbox/Advances in Political Psychology Article/ANES 2024 Data/ap-pooled-weighted.pdf", width=6, height=4)

wav_time_fig_pid <- for_fig %>%
  group_by(year, pid_dum) %>%
  summarise(weighted_av_ap = weighted.mean(AP, weight_full, na.rm=TRUE),
            sd = sd(AP, na.rm=TRUE),
            se = sd / sqrt(n()))


a<-ggplot(wav_time_fig_pid, aes(x=year, y=weighted_av_ap, color=pid_dum)) +
  geom_point() +
  geom_line() +
  theme_bw() +
  scale_color_manual(values = c("black", "grey")) +  
  geom_errorbar(aes(ymin=weighted_av_ap - 1.96*se,
                    ymax=weighted_av_ap + 1.96*se),
                width=0.4) +
  labs(x="Year", y="In-Party - Out-Party FT", 
       # title = "Average Affective Polarization by Party",
       subtitle="A",
       caption="ANES (N=44,009),
Means and 95% CIs", color="")+
  scale_y_continuous(limits=c(0,100)) +
  theme(legend.position = "bottom")
a

############################################
#in and out
inoutanimus <- anes3 %>%
  select(year, pid_dum, inparty_FT, outparty_FT, weight_full) %>%
  drop_na() %>%
  pivot_longer(cols = inparty_FT:outparty_FT, names_to = "type", values_to = "FT")

animus <- inoutanimus %>%
  group_by(year, type) %>%
  summarise(weighted_av = weighted.mean(FT, weight_full, na.rm=TRUE),
            sd = sd(FT, na.rm=TRUE),
            se = sd / sqrt(n()))

##change type
animus$type <- ifelse(animus$type=="inparty_FT", "In-Party FT", "Out-Party FT")

p2 <- ggplot(animus, aes(year, weighted_av)) +
  geom_line(aes(linetype=type)) +
  geom_errorbar(aes(ymax=weighted_av + 1.96*se, ymin=weighted_av-1.96*se), width=0.4)+
  theme_bw()+
  scale_linetype_manual(values=c(2,3))+
  scale_y_continuous(limits=c(0,100)) +
  labs(x="Year", y="Feeling Thermometer Rating", 
       subtitle ="B",
       caption="ANES (N=44,009),
Means and 95% CIs", linetype="") +
  theme(legend.position = "bottom")


#pdf(file="figure-1.pdf",
 #   height = 8.5,
  #  width = 11)
#ggarrange(p1,p2,
 #         nrow = 1, ncol = 2, common.legend = T, legend = "bottom")
#dev.off() 


png(file = "dot_code_figs/figs/figure-1.png",
    height = 8.5,
    width = 11,
    units = "in",       # Use inches to match pdf dimensions
    res = 300)          # Set resolution (DPI) for high-quality output

print(ggarrange(p1,p2,
                nrow = 1, ncol = 2, common.legend = T, legend = "bottom"))  

dev.off()



animusparty <- inoutanimus %>%
  group_by(year, type, pid_dum) %>%
  summarise(weighted_av = weighted.mean(FT, weight_full, na.rm=TRUE),
            sd = sd(FT, na.rm=TRUE),
            se = sd / sqrt(n()))

##change type
animusparty$type <- ifelse(animusparty$type=="inparty_FT", "In-Party FT", "Out-Party FT")

b<- ggplot(animusparty, aes(year, weighted_av, color=pid_dum)) +
  geom_point()+
  geom_line(aes(linetype=type)) +
  geom_errorbar(aes(ymax=weighted_av + 1.96*se, ymin=weighted_av-1.96*se), width=0.4)+
  theme_bw()+
  scale_color_manual(values=c("black", "grey"))+
  scale_y_continuous(limits=c(0,100)) +
  labs(x="Year", y="Feeling Thermometer Rating", 
       subtitle="B",
       caption="ANES (N=44,009),
Means and 95% CIs", linetype="", color="") +
#scale_linetype_manual(values = c("solid", "dashed"), guide="none")+
    theme(legend.position = "bottom")
b 

#pdf(file="C:/Users/sarah/Dropbox/Advances in Political Psychology Article/ANES 2024 Data/figure-4.pdf",
 #   height = 8.5,
  #  width = 11)
#ggarrange(a,b,
 #         nrow = 1, ncol = 2, common.legend = F, legend = "bottom")
#dev.off() 


png(file = "dot_code_figs/figs/figure-4.png",
    height = 8.5,
    width = 11,
    units = "in",       # Use inches to match pdf dimensions
    res = 300)          # Set resolution (DPI) for high-quality output

print(ggarrange(a,b,
                nrow = 1, ncol = 2, common.legend = F, legend = "bottom"))  

dev.off()

